package com.example.huanhang.changzhouapp.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.huanhang.changzhouapp.R;
import com.example.huanhang.changzhouapp.activity.FeiLeiActivity;
import com.example.huanhang.changzhouapp.activity.NoticeActivity;
import com.example.huanhang.changzhouapp.adapter.ListAllAdapter;
import com.example.huanhang.changzhouapp.base.BaseLazyFragment;
import com.example.huanhang.changzhouapp.bean.ListInfo;
import com.example.huanhang.changzhouapp.util.ThreadUtil;
import com.example.huanhang.changzhouapp.util.ViewFindUtils;
import com.example.huanhang.changzhouapp.view.CustomTextView;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author : lei
 * @desc :
 * @date : 2018/9/4 0004  下午 2:28.
 * 个人博客站: http://www.bestlei.top
 */

public class HomeFragment extends BaseLazyFragment implements View.OnClickListener, OnRefreshListener {

    @BindView(R.id.tv_title)
    AppCompatTextView mTvTitle;
    @BindView(R.id.title)
    RelativeLayout mTitle;
    @BindView(R.id.in_recycleview)
    RecyclerView mInRecycleview;
    @BindView(R.id.in_refresh)
    SmartRefreshLayout mInRefresh;
    private ListAllAdapter mAdapter;
    private List<ListInfo> mList = new ArrayList<>();
    private CustomTextView marqueeView;
    private CustomTextView mCustomTextView;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ImmersionBar.setTitleBar(getActivity(), mTitle);

    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.statusBarColorTransformEnable(false)
                .init();
    }

    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setText("工作");
        mInRefresh.setEnableAutoLoadMore(false);
        mInRefresh.setOnRefreshListener(this);
        mInRefresh.setRefreshHeader(new ClassicsHeader(getActivity()));
        setAdapterData();
        mInRecycleview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new ListAllAdapter(R.layout.item_list_layout, mList);
        adapterClickInfo();
        mInRecycleview.setAdapter(mAdapter);
        mAdapter.addHeaderView(getHeaderView());
    }
    private void setAdapterData() {
        initNoticeData();
    }

    private void adapterClickInfo() {
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ListInfo listInfo = mList.get(position);
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            View v = inflater.inflate(R.layout.dialog_layout, null);
            TextView title = v.findViewById(R.id.title);
            TextView date = v.findViewById(R.id.date);
            TextView name = v.findViewById(R.id.name);
            TextView content = v.findViewById(R.id.content);
            TextView tvok = v.findViewById(R.id.tvok);
            title.setText(listInfo.getTitle());
            date.setText(listInfo.getDate());
            name.setText(listInfo.getName());
            content.setText(listInfo.getContent());
            final Dialog dialog = builder.create();
            dialog.show();
            dialog.getWindow().setContentView(v);
            tvok.setOnClickListener(v1 -> dialog.dismiss());
        });
    }

    /**
     * 通知公告数据
     */
    private void initNoticeData() {
        if (mList.size() != 0) {
            mList.clear();
        }
        showWaitDialog(getActivity(),"查询中...");
        ThreadUtil.runOnBackThread(() -> {
            String sql = "SELECT * FROM gonggao ORDER BY lccb_time DESC";
            try {
                ResultSet result = ThreadUtil.result(sql);
                while (result.next()) {
                    ListInfo listInfo = new ListInfo();
                    listInfo.setName(result.getString("lccb_name"));
                    listInfo.setDate(result.getString("lccb_time"));
                    listInfo.setTitle(result.getString("lccb_title"));
                    listInfo.setContent(result.getString("lccb_content"));
                    mList.add(listInfo);
                }
                querySuccess();
                result.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    private void querySuccess() {
        ThreadUtil.runOnUiThread(() -> {
            dismissWaitDialog();
            mAdapter.notifyDataSetChanged();
        });

    }


    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        if (mList.size() != 0) {
            mList.clear();
        }
        initNoticeData();
        mInRefresh.finishRefresh();
    }

    private View getHeaderView() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View headerView = inflater.inflate(R.layout.home_fragment_header, null);
        ViewFindUtils.find(headerView, R.id.home_create).setOnClickListener(this);
        ViewFindUtils.find(headerView, R.id.home_mine).setOnClickListener(this);
        mCustomTextView = ViewFindUtils.find(headerView, R.id.tip_text);
        mCustomTextView.setOnClickListener(this);
        marqueeView = headerView.findViewById(R.id.marqueeView);
        initMarqeeView();
        return headerView;
    }

    /**
     * 公告跑马灯
     */
    private void initMarqeeView() {
        marqueeView.setOnClickListener(this);
        marqueeView.setText("榕盛公司始终坚持“精益求精、诚信服务”的企业理念，在产品研发和技术层面精益求精，孜孜不倦，从而快速响应客户，提升服务质量。客户的需求是我们努力的方向，客户的满意是我们成功的目标！");
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_create:
                //通知公告
                Intent inte = new Intent(getActivity(), NoticeActivity.class);
                startActivity(inte);
                break;
            case R.id.home_mine:
                //任务
                Intent intent = new Intent(getActivity(), FeiLeiActivity.class);
                startActivity(intent);
                break;
            case R.id.tip_text:
                new AlertDialog.Builder(getActivity()).setTitle("详情查看")
                        .setIcon(R.mipmap.see)
                        .setMessage(mCustomTextView.getText())
                        .setPositiveButton("确定", null).show();
                break;
            case R.id.marqueeView:
                new AlertDialog.Builder(getActivity()).setTitle("详情查看")
                        .setIcon(R.mipmap.see)
                        .setMessage(marqueeView.getText())
                        .setPositiveButton("确定", null).show();
                break;
        }
    }


}
